/**
 * Created by shenc on 2016/1/29.
 */

//公共函数
var utils = {};

/**
 * Store a new settings in the browser
 *
 * @param String name Name of the setting
 * @param String val Value of the setting
 * @returns void
 */
utils.setStore = function (name, val) {
    if (typeof (Storage) !== "undefined") {
        localStorage.setItem(name, val);
    } else {
        setCookie(name, val, 'd30');
    }
};

/**
 * Get a prestored setting
 *
 * @param String name Name of of the setting
 * @returns String The value of the setting | null
 */
utils.getStore = function (name) {
    if (typeof (Storage) !== "undefined") {
        return localStorage.getItem(name);
    } else {
        return getCookie(name);
    }
};

/**
 * 根据字符串换算毫秒
 * @param str
 * @returns {*}
 */
utils.getSec = function (str) {
    var str1 = str.substring(1, str.length) - 0;
    var str2 = str.substring(0, 1);
    var obj = {
        "s": str1 * 1000,
        "h": str1 * 60 * 60 * 1000,
        "d": str1 * 24 * 60 * 60 * 1000
    };
    return obj[str2];
};

/**
 * cookies操作
 * setCookie("name","hayden","s20");
 * s20是代表20秒
 * h是指小时，如12小时则是：h12
 * d是天数，30天则：d30
 */
utils.setCookie = function (name, value, time, path, domain) {
    var _expires, _path, _domain;
    if (time) {
        var strSec = utils.getSec(time) - 0,
            exp = new Date();
        exp.setTime(exp.getTime() + strSec);
        _expires = ";expires=" + exp.toUTCString()
    } else {
        _expires = "";
    }
    _path = path ? ";path=" + path : ";path=/";
    _domain = domain ? ";domain=" + domain : "";
    document.cookie = name + "=" + encodeURI(value) + _expires + _path + _domain;
};

/**
 * 读取cookies
 * @param name
 * @returns {*}
 */
utils.getCookie = function (name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))
        return decodeURI(arr[2]);
    else
        return null;
};

/**
 * 数组去重
 * @param arr
 * @returns {Array}
 */
utils.unique = function (arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
        if (!hash[elem]) {
            result.push(elem);
            hash[elem] = true;
        }
    }
    return result;
};

/**
 * daterangepicker 初始化
 * @param jqBtn 触发按钮
 * @param jqInput 表单元素
 * @param dateArr 初始化值 开始  结束
 * @param showFormat 显示格式化
 */
utils.datePredefinedRanges = function (jqBtn, jqInput, dateArr,showFormat) {
    //var timeArr = [moment().subtract(29, 'days'), moment()];
    var timeArr = [];
    var showItem= $('.show-span', jqBtn);

    //处理开始时间
    if (typeof dateArr != 'undefined' && dateArr.length) {
        var strArr = dateArr.split('|');

        for(var i=0;i<strArr.length;i++){
            timeArr[i] = moment(strArr[i]);
        }
    }

    function cb(start, end) {
        //显示赋值
        var startDate = start.format(showFormat||'YYYY/MM/DD'), endDate = end.format(showFormat||'YYYY/MM/DD');
        if (startDate == endDate) {
            showItem.html(startDate);
        } else {
            showItem.html(startDate + ' -- ' + endDate);
        }

        //input 赋值
        jqInput.val(start.format('YYYY-MM-DD HH:mm:ss') + '|' + end.format('YYYY-MM-DD HH:mm:ss'));
    }

    if(timeArr.length>1){
        cb(timeArr[0], timeArr[1]);

        jqBtn.daterangepicker({
            ranges: {
                '今天': [moment(), moment()],
                '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                '最近一周': [moment().subtract(6, 'days'), moment()],
                '最近30天': [moment().subtract(29, 'days'), moment()],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            startDate: timeArr[0],
            endDate: timeArr[1]
        }, cb);
    }else{
        jqBtn.daterangepicker({
            ranges: {
                '今天': [moment(), moment()],
                '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                '最近一周': [moment().subtract(6, 'days'), moment()],
                '最近30天': [moment().subtract(29, 'days'), moment()],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }
        }, cb);
    }

};